﻿using System.Collections.Generic;
using System.Linq;
using BruteForce.Characters;
using BruteForce.NewManagers;
using BruteForce.Screens.CreateNewCharacterScreen;
using BruteForce.Screens.PlayingScreen.Map;
using BruteForce.Screens.PlayingScreen.Panels;
using Microsoft.Xna.Framework;

namespace BruteForce.GameObjects
{
    public enum PlayerType
    {
        Human,
        Monster,
        None
    }

    public class Player
    {
        public List<Character> Characters;
        public Character CurrentCharacter;
        public Resources CurrentResources;
        public Color PlayerColor;
        public PlayerType PlayerType;
        public Resources ResourcesPerTurn;
        public List<MapCell> Towers;


        public Player(PlayerType playerType, Color color, Resources initRes, Resources resPerTurn,
                      Character initCharacter)
        {
            PlayerType = playerType;
            PlayerColor = color;
            Characters = new List<Character>();
            CurrentResources = initRes;
            ResourcesPerTurn = resPerTurn;
            
            CurrentCharacter = initCharacter;
            AddCharacter(initCharacter);

            Towers = new List<MapCell>();
            AddTower(initCharacter.MapCell);
        }


        public void AddTower(MapCell tower)
        {
            Towers.Add(tower);
            ResourcesPerTurn.AddResources(tower.Resources);
        }

        public void RemoveTower(MapCell tower)
        {
            Towers.Remove(tower);
            ResourcesPerTurn.RemoveResources(tower.Resources);
        }


        public void AddCharacter(Character character)
        {
            Characters.Add(character);
            CurrentResources.RemoveResources(CharacterCreationLogic.GetNeededResources(character.Type,character.InitialResources));
            GamePanel.Current.InsertCharacterToMap(character);
            //CurrentCharacter = character;
        }


        public void InitTurn()
        {
            foreach (var character in Characters)
            {
                character.InitTurn();
            }
            CorrectResources();
            CurrentCharacter = NullCharacter.Instance;
        }


        private void CorrectResources()
        {
            CurrentResources.AddResources(ResourcesPerTurn);
               foreach (var c in this.Characters.ToArray())
            {
                CurrentResources.Life -= c.InitialResources.Life;
                if (CurrentResources.Life < 0)
                {
                    CurrentResources.Life = 0;
                    c.DecreaseLife(1);
                }
            }
        }

        public int GetSumLifesOfAllCharacters()
        {
            var ans = 0;
            foreach (var c in this.Characters)
            {
                ans += c.CurrentResources.Life;
            }
            return ans;
        }


        public void RemoveGamePiece(Character character)
        {
            CurrentCharacter = NullCharacter.Instance;
            Characters.Remove(character);
        }





        

        //public void Fire(Tile toTile) {
        //    //foreach (GamePiece gamePiece in toTile.ContainedGamePieces) {
        //    //if (!gamePiece.BelongsToPlayer.Equals(PlayerId)
        //    //    & !gamePiece.BelongsToPlayer.Equals(GameManager.ComputerId)) {
        //    if (CurrentCharacter is Shooter)
        //        CurrentCharacter.Fire(CurrentGamePieceTile, toTile);
        //    else if (CurrentCharacter is SuicideBomber)
        //        Explode();
        //    //}
        //    //}

        //}

        //private void Explode() {
        //    CurrentCharacter.Fire(CurrentGamePieceTile, CurrentGamePieceTile);
        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="tile">The tile the mouse clicked</param>
        //public void MouseClicked(Tile tile) {
        //    switch (PlayersManager.InputState) {
        //        case State.Selection:
        //            if (SelectGamePiece(tile))
        //                PlayersManager.InputState = State.Walking;
        //            break;

        //        case State.Walking:
        //            MoveGamePiece(CurrentGamePieceTile, tile, tile.IsHighlighted());
        //            PlayersManager.InputState = State.Selection;
        //            break;

        //        case State.Firing:
        //            Fire(tile);
        //            break;

        //        case State.GeneratingNewGamePiece:
        //            GenerateNewPiece(tile);
        //            PlayersManager.InputState = State.Selection;
        //            break;
        //    }
        //}

        //private void GenerateNewPiece(Tile tile) {

        //    if (tile.ContainedGamePieces.Count == 0) {
        //        GamePiece gamePiece = DashboardManager.GenerateNewCharacter(tile);
        //        gamePiece.BelongsToPlayer = PlayerId;
        //        tile.ContainedGamePieces.Add(gamePiece);
        //        AddGamePiece(gamePiece, tile);
        //    }

        //}

        public void CheckIfLost()
        {
            if (Characters.OfType<Defender>().Any()){return;}
            GamePanel.Current.GameOver(this);
            //todo call the losing function
            
        }
    }
}